/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */


package com.smg.community.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseRestSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.org.rapid_framework.page.Page;

import com.smg.community.model.CrmRight;
import com.smg.community.model.ExtJsPageData;
import com.smg.community.vo.query.CrmRightQuery;
/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/crmright")
public class CrmRightController extends BaseRestSpringController<CrmRight,java.lang.Integer>{
	
	private String retCode; // 返回code
	private String retMsg; // 返回信息
	
	@RequestMapping("/listmenu")
	public void listmenu(HttpServletRequest request, HttpServletResponse response, ModelMap model){
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			String nodeParentId = request.getParameter("nodeParentId");
			if(null == nodeParentId || "".equals(nodeParentId)){
				nodeParentId = "0";
			}
			String roleIds = request.getParameter("roleIds");
			roleIds = (null == roleIds || "".equals(roleIds))? "0": roleIds.substring(1,roleIds.length() - 1);
			roleIds = "".equals(roleIds) ? "0" : roleIds;
			List<CrmRight> list = crmRightManager.getAllRightByRoleId(roleIds,nodeParentId);
			
			StringBuffer jsonbf = new StringBuffer();
			jsonbf.append("[");
			for(CrmRight right : list){
				if("function".equals(right.getType())){
					continue;
				}
			    String ischildren="";
		        //如果涉及多节点请封装或者查数据库
			    if(null != right && "0".equals(right.getParentCode())){
		           ischildren="expanded: false";
		        }else{
				   ischildren="leaf:true";
		        }
		         	jsonbf.append("{'code':'" + right.getCode()
		    		+ "','parent_code':'"+ right.getParentCode()
		            + "','text':'" + right.getText()
		            + "','url':'" + right.getUrl()
		            + "',"+ischildren+"},");
	    	}
			response.getWriter().write(jsonbf.substring(0, jsonbf.length()-1) + "]");
			response.flushBuffer();   
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}
	
	/**
	 * 列表显示地址
	 */
	@RequestMapping("/show")
	public String show () {
		return "/crmright/list_right";
	}
	
	/**
	 *分页显示数据
	 */
	@RequestMapping("/ajax/list")
	public @ResponseBody ExtJsPageData<CrmRight>  index(HttpServletResponse response, HttpServletRequest request,CrmRightQuery query) {
		setPagerModel(query,request);
		Page<CrmRight> page = crmRightManager.findPage(query);
		return transToExtJsPageData(page.getResult(),page.getTotalCount());
	}
	/**
	 * 获取子权限
	 */
	@RequestMapping("/ajax/getSubPermission")
	public void getSubPermission (HttpServletResponse response, HttpServletRequest request) {
		String id = request.getParameter("id"); // 主键
		List<CrmRight> crmRights = null;
		try {
			crmRights = crmRightManager.selectChidrenPers(id);
			retCode = "S";
		} catch (Exception  e) {
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("retCode", retCode);
		paramMap.put("retMsg", retMsg);
		paramMap.put("crmRights", crmRights);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			writeMessage(out, paramMap);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	/**
	 * 移动权限的顺序
	 */
	@RequestMapping("/ajax/updateSort")
	public void updateSort (HttpServletResponse response, HttpServletRequest request) {
		String sortCount = request.getParameter("sortCount"); // 移动位数
		String id = request.getParameter("id"); // 主键
		try {
			retMsg = crmRightManager.updateSort(id, sortCount);
			retCode = "S";
		} catch (Exception e) {
		}
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("retCode", retCode);
		paramMap.put("retMsg", retMsg);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			writeMessage(out, paramMap);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	/**
	 * 新增权限
	 */
	@RequestMapping("/ajax/addRightPer")
	public void addRightPer (HttpServletResponse response, HttpServletRequest request) {
		String text = request.getParameter("text");
		String rightDesc = request.getParameter("rightDesc");
		String parentId = request.getParameter("parentId");
		String url = request.getParameter("url");
		try {
			retMsg = crmRightManager.addRightPer(text, rightDesc, parentId, url);
			retCode = "S";
		} catch (Exception e) {
		}
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("retCode", retCode);
		paramMap.put("retMsg", retMsg);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			writeMessage(out, paramMap);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	public String getRetCode() {
		return retCode;
	}

	public void setRetCode(String retCode) {
		this.retCode = retCode;
	}

	public String getRetMsg() {
		return retMsg;
	}

	public void setRetMsg(String retMsg) {
		this.retMsg = retMsg;
	}
}

